home *** CD-ROM | disk | FTP | other *** search
/ JCSM Shareware Collection 1996 September / JCSM Shareware Collection (JCS Distribution) (September 1996).ISO / bother__ / cenvid.zip / CENVIDOS.ZIP / DELTREE.BAT < prev    next >
DOS Batch File  |  1995-04-07  |  5KB  |  118 lines

  1. @echo off
  2. REM *******************************************************************
  3. REM *** DelTree - Delete a subdirectory and all files in it, and    ***
  4. REM *** ver.2     recursively delete all subdirectories within that ***
  5. REM ***           subdirectory.  You may look at DelTree1.bat and   ***
  6. REM ***           DelTree2.bat for other methods of doing the       ***
  7. REM ***           same thing by using temporary files.              ***
  8. REM *******************************************************************
  9. if "%1"=="" GOTO SHOW_HOW
  10.  
  11. REM *******************************************************************
  12. REM *** The user could be very unhappy to delete lots of files they ***
  13. REM *** didn't really want to delete, and so give them a chance to  ***
  14. REM *** change their mind.                                          ***
  15. REM *******************************************************************
  16.    CEnviD return( stricmp(`NOASK`,`%2`) ? 0 : 1 )
  17.    IF ERRORLEVEL 1 GOTO ANSWER_YES
  18.    ECHO THIS WILL DELETE ALL FILES AND DIRECTORIES UNDER %1
  19.    CEnviD GetUKey.cmm ARE YOU SURE YOU WANT TO DO THIS? (Y/N)    yn
  20.    if N==%UKEY% GOTO FINI
  21.    :ANSWER_YES
  22.  
  23. REM *********************************************************************
  24. REM *** Check that this is a valid subdirectory.  This can be handled ***
  25. REM *** by calling another CEnvi program: ValidDir.bat                ***
  26. REM *********************************************************************
  27.    CEnviD ValidDir.bat %1 COMPLAIN
  28.    if ERRORLEVEL 1 GOTO FINI
  29.  
  30. REM ******************************************************************
  31. REM *** It will be simpler to delete files if we make sure that no ***
  32. REM *** attributes are set that would make them undeletable.  The  ***
  33. REM *** easy way to change this is to let the DOS attrib function  ***
  34. REM *** clear all of these attributes for us.                      ***
  35. REM ******************************************************************
  36.    attrib -H -S -R %1\*.* /s > NUL
  37.  
  38. REM **************************************************************************
  39. REM *** Temporarily turn off DELDIR to make this deletion fast (if unsafe) ***
  40. REM **************************************************************************
  41. set DELTREE_SAVE_DEL_DIR=%DELDIR%
  42. set DELDIR=
  43.  
  44. REM ******************************************************************
  45. REM *** The rest of this task will be handled by CEnviD which will ***
  46. REM *** create a list of all directories and then call the command ***
  47. REM *** shell to delete the files in each directory.               ***
  48. REM ******************************************************************
  49. CEnviD %0.bat %1
  50. GOTO CENVI_EXIT
  51.  
  52.    main(argc,argv)
  53.    {
  54.       // build a sorted list of all subdirectories under the specified directory
  55.       DirCount = BuildSubdirList(argv[1],DirList)
  56.       if ( DirCount != 0 ) {
  57.          // sort the subdir list in reverse alphabetic order, which insures that
  58.          // subdirectoies of a directory appear before its parent directory
  59.          qsort(DirList,DirCount,"ReverseSortDirnames")
  60.          // for each subdirectory, call command shell to delete all files in it
  61.          // and then remove the directory
  62.          for ( i = 0; i < DirCount; i++ ) {
  63.             DeleteAllFilesInDir( DirList[i].name );
  64.             RemoveDirectory( DirList[i].name );
  65.          }
  66.       }
  67.       // all subdirectories have have been deleted, then delete the input subdir
  68.       DeleteAllFilesInDir( argv[1] );
  69.       RemoveDirectory( argv[1] );
  70.    }
  71.  
  72.    BuildSubdirList(BaseDir,List) // build List of all subdirectories under BaseDir
  73.    {                             // return count of elements in list
  74.       sprintf(SearchSpec,"%s\\*.*",BaseDir)
  75.       List = Directory(SearchSpec,TRUE,
  76.                        FATTR_RDONLY|FATTR_HIDDEN|FATTR_SYSTEM|FATTR_SUBDIR|FATTR_ARCHIVE,
  77.                        FATTR_SUBDIR)
  78.       return( ( List == NULL ) ? 0 : 1+GetArraySpan(List) );
  79.    }
  80.  
  81.    ReverseSortDirnames(Dir1,Dir2) // repeatedly called by qsort to sort Dir1 and Dir2
  82.    {
  83.       return( stricmp(Dir2.name,Dir1.name) );
  84.    }
  85.  
  86.    DeleteAllFilesInDir(DirName) // call system to delete all files in this dir
  87.    {
  88.       printf("DEL %s\n",DirName)
  89.       system("echo Y | del %s > NUL",DirName)
  90.    }
  91.  
  92.    RemoveDirectory(DirName) // call system RMDIR call
  93.    {
  94.       printf("RMDIR %s\n",DirName)
  95.       system("RMDIR %s > NUL",DirName)
  96.    }
  97.  
  98. :CENVI_EXIT
  99. REM **********************
  100. REM *** Restore DELDIR ***
  101. REM **********************
  102. set DELDIR=%DELTREE_SAVE_DEL_DIR%
  103. set DELTREE_SAVE_DEL_DIR=
  104. GOTO FINI
  105.  
  106. :SHOW_HOW
  107.    ECHO .
  108.    ECHO DelTree.bat - Delete a subdirectory and all files and directories under it
  109.    ECHO .
  110.    ECHO USAGE: DelTree DirSpec [NOASK]
  111.    ECHO WHERE: NOASK: (optional) to not prompt "are you sure?"
  112.    ECHO        DirSpec: Specification for directory to delete
  113.    ECHO .
  114. GOTO FINI
  115.  
  116. :FINI
  117. set UKEY=
  118.